package com.share.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.share.domain.Angle;
import com.share.domain.Catelog;
import com.share.domain.Content;
import com.share.domain.Result;
import com.share.service.AngleService;
import com.share.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: share-admin
 * 描述：
 * @author: XLX
 * @create: 2022-12-06 14:48
 **/
@RestController
@RequestMapping("/sys/angle")
public class AngleController extends BaseController {

    @Autowired
    AngleService angleService;


    @Autowired
    ContentService contentService;


   @GetMapping("/selectAngleList")
   public Result selectAngleList(@RequestParam(required = false) Long lexId){
       if (lexId!=null){
           List<Content> cons = contentService.list(new QueryWrapper<Content>().eq("lex_id", lexId));
           if (cons.size()>=1){
               List<Integer> angleIds = cons.stream().map(c -> c.getAid()).collect(Collectors.toList());
               List<Angle> angles = angleService.listByIds(angleIds);
               return Result.succ(angles);
           }else {
               return Result.succ(new ArrayList<Angle>());
           }

       }
       return Result.succ(angleService.list());
   }


    @GetMapping("/findAngleByLexId")
    public Result findAngleByLexId(Integer lexId){

        Page<Angle> page = angleService.findAngleByLexId(lexId,getPage());
        return Result.succ(page);
    }



    @GetMapping("/info/{id}")
    public Result info(@PathVariable(name = "id") Long id){
        Angle angle = angleService.getById(id);

        return Result.succ(angle);
    }


    @GetMapping("/list")
    public Result list(String name){

        Page<Angle> page = angleService.page(getPage(),
                new QueryWrapper<Angle>().like(StrUtil.isNotBlank(name),"name",name));

        return Result.succ(page);
    }




    @PostMapping("/save")
    public Result save(@Validated @RequestBody Angle angle){

        angleService.save(angle);
        return Result.succ(angle);
    }


    @PostMapping("/update")
    public Result update(@Validated @RequestBody Angle angle){
        angleService.updateById(angle);
        return Result.succ(angle);
    }


    @PostMapping("/delete")
    @Transactional
    public Result delete(@RequestBody Long[] ids){
        //删除角度
        angleService.removeByIds(Arrays.asList(ids));

        return Result.succ("角度删除成功");

    }
}